$(function () {
    initLayerDate("#reg_begTime",false);
    initLayerDate("#reg_endTime",false);
    initLayerDate("#order_begTime",false);
    initLayerDate("#order_endTime",false);
    initLayerDate("#DUA_begTime",false);
    initLayerDate("#DUA_endTime",false);
    //-------创建Y轴数组
    var hoursArray =['0:00','1:00','2:00','3:00','4:00','5:00','6:00','7:00','8:00','9:00','10:00','11:00'
        ,'12:00','13:00','14:00','15:00','16:00','17:00','18:00','19:00','20:00','21:00','22:00','23:00'];
    //--------------时间，开始
    initIndexTime();
    //--------------时间，结束
    //加载基础数据统计
    getCountNumData(function (data) {
        var userCountMap = data.userCountMap;
        //注册用户总数
        $("#sumUserNum").text(userCountMap.sumUserNum);
        //付费用户总数
        $("#buyUserNum").text(userCountMap.buyUserNum);

        var courseNumMap = data.courseNumMap;
        //课程总数
        $("#sumCourseNum").text(courseNumMap.sumCourseNum);
        //总直播课节
        $("#liveNodeNum").text(courseNumMap.liveNodeNum);

        var examCountNumMap = data.examCountNumMap;
        //试题总数
        $("#sumQuestionNum").text(examCountNumMap.sumQuestionNum);
        //试卷总数
        $("#sumExamNum").text(examCountNumMap.sumExamNum);
    });


    $('body').on('click','.reg',function(){
        var params = $('#reg-form').serializeJSON();
        var number = judgeState("reg-form");
        if(number==1){
            queryUserRegDayTimeNum(queryUserRegDayTimeNumCallback,"");
        }else if(number==2){
            queryUserRegDayTimeNum(queryUserRegDayTimeNumCallback,params.begTime);
        }else  if(number==3){
            queryUserRegTimeNum(queryUserRegDayTimeNumCallback);
        }

    }).on('click','.order',function(){
        var params = $('#order-form').serializeJSON();
        var number = judgeState("order-form");
        if(number==1){
            queryOrderDayTimeNum(queryOrderDayTimeNumCallback,"");
        }else if(number==2){
            queryOrderDayTimeNum(queryOrderDayTimeNumCallback,params.begTime);
        }else  if(number==3){
            queryOrderDayNum(queryOrderDayTimeNumCallback);
        }
    }).on('click','.dua',function(){
        getConversionRate();
    });

    /**
     * 判断搜索状态
     * @param id
     * @returns {number}
     */
    function judgeState (id){
        var params = $('#'+id).serializeJSON();
        if(params.begTime==""&&params.endTime==""){
            return 1;
        }
        var oDate1 = new Date(params.begTime);
        var oDate2 = new Date(params.endTime);
        if(oDate1.getTime() == oDate2.getTime()){
            return 2;
        } else {
            return 3;
        }

    }
    /**
     * 加载基础数据统计
     * @param callback
     */
    function getCountNumData(callback) {
        $.ajax({
            url:baseloaction +'/admin/count/getCountNumData',
            type:'post',
            dataType:'json',
            success:function (result) {
                if(result.success==true){
                    callback(result.entity);
                }
            }
        });
    }

    //加载今日网站注册量数据
    queryUserRegDayTimeNum(queryUserRegDayTimeNumCallback,"");
    //网站注册量曲线-----------开始
    /**
     * 加载当日注册量的统计数据
     * @param callback
     */
    function queryUserRegDayTimeNum(callback,date) {
        $.ajax({
            url:baseloaction +'/admin/count/queryUserRegDayTimeNum',
            type:'post',
            data:{"date":date},
            dataType:'json',
            success:function (result) {
                if(result.success==true){
                    callback(result,1);
                }
            }
        });
    }

    /**
     * 加载某时间端段的注册曲线
     * @param callback
     */
    function queryUserRegTimeNum(callback) {
        var params = $('#reg-form').serializeJSON();
        params = JSON.stringify(params);
        $.ajax({
            url:baseloaction +'/admin/count/queryUserDayTimeNum',
            type:'post',
            data:params,
            dataType:'json',
            contentType:'application/json;charset=utf-8',
            success:function (result) {
                if(result.success==true){
                    callback(result,2);
                }
            }
        });
    }

    function queryUserRegDayTimeNumCallback(result,type){
        var max = result.max;
        var list = result.entity;
        var regUserNumArr = new Array();
        var yArray = new Array();
        if(type==1){
            yArray =hoursArray;
        }
        for(var i=0;i<list.length;i++){
            regUserNumArr.push(list[i].regNum);
            if(type==2){
                yArray.push(list[i].date)
            }
        }
        var lineChart = echarts.init(document.getElementById("reg-echarts-line-chart")),
            colors = ['#62A1FE', '#4ECE39', '#675bba'];
        var lineoption = {
            color: colors,
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    type: 'cross',
                    crossStyle: {
                        color: '#999'
                    }
                }
            },
            toolbox: {
                feature: {
                    dataView: {show: true, readOnly: false},
                    magicType: {show: true, type: ['line', 'bar']},
                    restore: {show: true},
                    saveAsImage: {show: true}
                }
            },
            legend: {
                data:['注册用户']
            },
            xAxis: [
                {
                    type: 'category',
                    data: yArray,
                    axisPointer: {
                        type: 'shadow'
                    }
                }
            ],
            yAxis: [
                {
                    type: 'value',
                    name: '注册用户',
                    min: 0,
                    max: max,
                    interval: 50,
                    axisLabel: {
                        formatter: '{value} 个'
                    }
                }
            ],
            series: [
                {
                    name:'注册用户',
                    type:'line',
                    data:regUserNumArr
                }
            ]
        };
        lineChart.setOption(lineoption);
        $(window).resize(lineChart.resize);
    }

    //今日网站订单量曲线-----------开始
    queryOrderDayTimeNum(queryOrderDayTimeNumCallback,"");
    /**
     * 加载今日网站订单量统计数据
     * @param callback
     */
    function  queryOrderDayTimeNum(callback,date) {
        $.ajax({
            url:baseloaction +'/admin/count/queryOrderNowDayNum',
            type:'post',
            dataType:'json',
            data:{"date":date},
            success:function (result) {
                if(result.success==true){
                    callback(result,1);
                }
            }
        });
    }
    /**
     * 加载某时间端段的订单曲线
     * @param callback
     */
    function queryOrderDayNum(callback) {
        var params = $('#order-form').serializeJSON();
        params = JSON.stringify(params);
        $.ajax({
            url:baseloaction +'/admin/count/queryOrderDayNum',
            type:'post',
            data:params,
            dataType:'json',
            contentType:'application/json;charset=utf-8',
            success:function (result) {
                if(result.success==true){
                    callback(result,2);
                }
            }
        });
    }
//显示数据统计
    function queryOrderDayTimeNumCallback(result,type){
        var max = result.max;
        var list = result.entity;
        var orderDayInitNumArr = new Array();
        var orderDaySuccessNumArr = new Array();
        var yArray = new Array();
        if(type==1){
            yArray =hoursArray;
        }
        for(var i=0;i<list.length;i++){
            orderDayInitNumArr.push(list[i].initNum);
            orderDaySuccessNumArr.push(list[i].successNum);
            if(type==2){
                yArray.push(list[i].date)
            }
        }
        var lineChart = echarts.init(document.getElementById("order-echarts-line-chart")),
            colors = ['#62A1FE', '#4ECE39', '#675bba'];
        var lineoption = {
            color: colors,
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    type: 'cross',
                    crossStyle: {
                        color: '#999'
                    }
                }
            },
            toolbox: {
                feature: {
                    dataView: {show: true, readOnly: false},
                    magicType: {show: true, type: ['line', 'bar']},
                    restore: {show: true},
                    saveAsImage: {show: true}
                }
            },
            legend: {
                data:['已支付','未支付']
            },
            xAxis: [
                {
                    type: 'category',
                    data: yArray,
                    axisPointer: {
                        type: 'shadow'
                    }
                }
            ],
            yAxis: [
                {
                    type: 'value',
                    name: '订单量',
                    min: 0,
                    max: max,
                    interval: 50,
                    axisLabel: {
                        formatter: '{value} 个'
                    }
                }
            ],
            series: [
                {
                    name:'已支付',
                    type:'line',
                    data:orderDaySuccessNumArr
                },
                {
                    name:'未支付',
                    type:'line',
                    data:orderDayInitNumArr
                }
            ]
        };
        lineChart.setOption(lineoption);
        $(window).resize(lineChart.resize);
    }
    //今日网站订单量曲线-----------结束

    //地图--------------------------开始
    /**
     * 加载用户注册地理分布数据统计
     * @param callback
     * @return {*}
     */
    function queryUserRegLayoutNum(callback) {
        $.ajax({
            url: baseloaction +'/admin/count/queryUserRegLayoutNum',
            type:'post',
            dataType:'json',
            async:false,
            success:function (result) {
                if(result.success==true){
                    callback(result);
                }
            }
        });
    }

    //加载显示数据
    queryUserRegLayoutNum(function (result) {
        var max = result.max;
        var userArr = new Array();
        var list = result.entity;
        for(var i=0;i<list.length;i++){
            var obj = {};
            obj.name=list[i].regAddress;
            obj.value=list[i].userNum;
            userArr.push(obj);
        }
        var mapChart = echarts.init(document.getElementById("user-echarts-map-chart"));
        var mapoption = {
                tooltip: {
                    trigger: 'item',
                    formatter: function (params) {
                        if(params.value){
                            return params.seriesName + '<br/>' + params.name + ' : ' + params.value;
                        }else{
                            return params.seriesName + '<br/>' + params.name + ' : ' + '0';
                        }

                    }
                },
            visualMap: {
                seriesIndex: 0,
                min: 0,
                max: max,
                left: 'left',
                top: 'bottom',
                text: ['高','低'],           // 文本，默认为数值文本
                calculable: true
            },
            grid: {
                height: 200,
                width: 8,
                right: 80,
                bottom: 10
            },
            xAxis: {
                type: 'category',
                data: [],
                splitNumber: 1,
                show: false
            },
            yAxis: {
                position: 'right',
                min: 0,
                max: 20,
                splitNumber: 20,
                inverse: true,
                axisLabel: {
                    show: true
                },
                axisLine: {
                    show: false
                },
                splitLine: {
                    show: false
                },
                axisTick: {
                    show: false
                },
                data: []
            },
            series: [
                {
                    zlevel: 1,
                    name: '注册地域 ',
                    type: 'map',
                    mapType: 'china',
                    selectedMode : 'multiple',
                    roam: true,
                    left: '8%',
                    right: '8%',
                    label: {
                        normal: {
                            show: true
                        },
                        emphasis: {
                            show: true
                        }
                    },
                    data:userArr
                }
            ]
        };
        mapChart.setOption(mapoption);
        $(window).resize(mapChart.resize);
    });
    //地图--------------------------结束


    //各端访问分布-------------------开始
    /**
     * 用户登录设备统计量
     */
    function queryUserLoginDeviceNum(callback) {
        $.ajax({
            url: baseloaction +'/admin/count/queryUserLoginDeviceNum',
            type:'post',
            dataType:'json',
            success:function (result) {
                if(result.success==true){
                    callback(result);
                }
            }
        });
    }

    //加载数据显示
    queryUserLoginDeviceNum(function (result) {
        var userLoginDeviceObj = result.entity;
        var pieChartData = new Array();
        if(userLoginDeviceObj!=null){
            var pcObj = {value:parseInt(userLoginDeviceObj.pcNum),name:'PC'};
            pieChartData.push(pcObj);
            var appObj = {value:parseInt(userLoginDeviceObj.appNum),name:'APP'};
            pieChartData.push(appObj);
            var mobileObj = {value:parseInt(userLoginDeviceObj.mobileNum),name:'H5'};
            pieChartData.push(mobileObj);
            var appletObj = {value:parseInt(userLoginDeviceObj.appletNum),name:'小程序'};
            pieChartData.push(appletObj);
        }
        var pieChart = echarts.init(document.getElementById("user-echarts-pie-chart"));
        var pieoption = {
            title : {
                text: '用户访问设备分布',
                subtext: '',
                x:'center'
            },
            tooltip : {
                trigger: 'item',
                formatter: "{a} <br/>{b} : {c} ({d}%)"
            },
            legend: {
                orient: 'vertical',
                left: 'left',
                data: ['PC','APP','H5','小程序']
            },
            series : [
                {
                    name: '访问来源',
                    type: 'pie',
                    radius : '55%',
                    center: ['50%', '60%'],
                    data:pieChartData,
                    itemStyle: {
                        emphasis: {
                            shadowBlur: 10,
                            shadowOffsetX: 0,
                            shadowColor: 'rgba(0, 0, 0, 0.5)'
                        }
                    }
                }
            ]
        };
        pieChart.setOption(pieoption);
        $(window).resize(pieChart.resize);
    });
    //各端访问分布-------------------结束

//运营概况
    getConversionRate();
    function getConversionRate() {
        var params = $('#dua-form').serializeJSON();
        params = JSON.stringify(params);
        $.ajax({
            url:baseloaction +'/admin/count/getConversionRate',
            type:'post',
            data:params,
            dataType:'json',
            contentType:'application/json;charset=utf-8',
            success:function (result) {
                var entity =result.entity;
                if(entity.activeDUA!=null && entity.activeDUA){
                    $("#activeDUA").text(entity.activeDUA+"%");
                }
                if(entity.orderConversionRate!=null && entity.orderConversionRate){
                    $("#orderConversionRate").text(entity.orderConversionRate+"%");
                }
                if(entity.registerConversionRate!=null && entity.registerConversionRate){
                    $("#registerConversionRate").text(entity.registerConversionRate+"%");
                }
                if(entity.sevenDayConversion!=null && entity.sevenDayConversion){
                    $("#sevenDayConversion").text(entity.sevenDayConversion+"%");
                }
                if(entity.thirtyDayConversion!=null && entity.thirtyDayConversion){
                    $("#thirtyDayConversion").text(entity.thirtyDayConversion+"%");
                }

            }
        });
    }
});